package com.hbsd.paopaobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.mapper.CommentMapper;
import com.hbsd.paopaobackend.model.domain.Comment;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Comment.AddCommentRequest;
import com.hbsd.paopaobackend.model.request.Comment.CommentPageRequest;
import com.hbsd.paopaobackend.model.vo.Comment.CommentPageVO;
import com.hbsd.paopaobackend.service.CommentService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author zzh
* @description 针对表【comment(评论表)】的数据库操作Service实现

*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {

    /**
     * 新增博客评论
     * @param addCommentRequest
     * @param loginUser
     * @return
     */
    @Override
    public Boolean addComment(AddCommentRequest addCommentRequest, User loginUser) {
        if (addCommentRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Long id = loginUser.getId();
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        String username = loginUser.getUsername();
        if (username == null || username.trim().isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        String content = addCommentRequest.getContent();
        if (content == null || content.trim().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评论内容不能为空");
        }
        // 设置评论用户信息
        addCommentRequest.setUsername(username);
        String avatarUrl = loginUser.getAvatarUrl();
        if (avatarUrl == null || avatarUrl.trim().isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }
        // 设置评论用户头像
        addCommentRequest.setUserAvatar(avatarUrl);
        // 设置评论用户ID
        addCommentRequest.setUserId(id);

        Comment comment = new Comment();
        try {
            BeanUtils.copyProperties(comment, addCommentRequest);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数转换失败");
        }

        boolean save = this.save(comment);
        if (!save) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "评论失败");
        }
        return save;

    }

    /**
     * 分页查询评论列表
     * @param commentPageRequest
     * @param loginUser
     * @return
     */
    @Override
    public Page<CommentPageVO> listCommentPage(CommentPageRequest commentPageRequest, User loginUser) {
        if (commentPageRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }
        Long blogId = commentPageRequest.getBlogId();
        if (blogId == null || blogId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "博客不存在");
        }

        long pageNum = commentPageRequest.getPageNum();
        long pageSize = commentPageRequest.getPageSize();

        if (pageNum <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        // 根据创建时间进行降序排序
        queryWrapper.orderByDesc("createTime");
        queryWrapper.eq("blogId", blogId);

        Page<Comment> commentPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);


        List<CommentPageVO> pageVOList = commentPage.getRecords().stream().map(comment -> {
            CommentPageVO commentPageVO = new CommentPageVO();
            try {
                BeanUtils.copyProperties(commentPageVO,comment);
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "转换失败");
            }

            return commentPageVO;
        }).collect(Collectors.toList());

        Page<CommentPageVO> page = new Page<>(pageNum, pageSize);
        // 根据创建时间进行降序排序
        page.setRecords(pageVOList);
        return page;

    }
}




